/*
 * CS_BUY_ITEM_CONSUME.cpp
 *
 *  Created on: 2013. 5. 28.
 *      Author: the2
 */

#include "../../Command.h"
#include "../../User.h"

namespace ProjectZ { namespace NCommand {

void SendFailBuyItemConsume(User * pUser, int res)
{
	::Network::Packet *pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(res);
	pAck->MakePacket(CMD_SC_BUY_ITEM_CONSUME);
	pUser->Send(pAck);
}

void Command::CS_BUY_ITEM_CONSUME(User * pUser, ::Network::Packet * pPacket)
{
/*	ZNO_CS_BUY_ITEM_CONSUME					0x2310
	{
		u32	u32buy_seq;
		u8	u8result;
		u32	u32err_code;
	}

	ZNO_SC_BUY_ITEM_CONSUME					0x2311
	{
		s16	s16ack;
	}*/

	PREPARE_PACKET;

	uint32_t buySeq		= 0;
	uint32_t errCode	= 0;
	char result			= 0;

	*pPacket >> buySeq >> result >> errCode;

	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::S_PROCEDURE);
	clsAuto->MakeQuery("call sp_update_BuyItemData_Consume(%d)", buySeq);

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
	{
		SendFailBuyItemConsume(pUser, ACK_DB_ERROR);
		return;
	}

	if( clsAuto->GetErrorIdx() != 0 )
	{
		SendFailBuyItemConsume(pUser, ACK_DB_ERROR);
		return;
	}

	::Network::Packet *pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ACK_OK);
	pAck->MakePacket(CMD_SC_BUY_ITEM_CONSUME);
	pUser->Send(pAck);
}

} /*NCommand*/ } /*ProjectZ*/
